from pydantic import BaseModel, Field, field_validator, model_validator, ConfigDict
from typing import Optional, List
from datetime import datetime, date


# ==================== Pydantic 模型定义 ====================

# 管理员登录请求模型
class AdminLogin(BaseModel):
    """管理员登录请求模型"""
    username: str
    password: str

# 管理员登录响应模型
class AdminLoginResponse(BaseModel):
    """管理员登录响应模型"""
    code: int
    msg: str
    access_token: Optional[str] = None
    token_type: Optional[str] = None
    admin_info: Optional[dict] = None

# 用户创建请求模型
class UserCreate(BaseModel):
    """用户创建请求模型"""
    username: Optional[str] = Field(None, max_length=50, description="用户名")
    password: Optional[str] = Field(None, max_length=255, description="密码")
    nickname: Optional[str] = Field(None, max_length=50, description="昵称")
    avatar: Optional[str] = Field(None, max_length=500, description="头像")
    gender: Optional[int] = Field(3, ge=1, le=3, description="性别 1-男 2-女 3-保密")
    age: Optional[int] = Field(0, ge=0, le=150, description="年龄")
    country: Optional[str] = Field(None, max_length=50, description="国家")
    province: Optional[str] = Field(None, max_length=50, description="省份")
    city: Optional[str] = Field(None, max_length=50, description="城市")
    district: Optional[str] = Field(None, max_length=50, description="区县")
    address: Optional[str] = Field(None, max_length=200, description="地址")
    role: Optional[int] = Field(1, ge=0, le=4, description="角色 1-普通用户 2-评委")
    birthday: Optional[str] = Field(None, description="生日")
    signature: Optional[str] = Field(None, max_length=200, description="签名")
    description: Optional[str] = Field(None, max_length=200, description="描述")
    phone: Optional[str] = Field(None, max_length=11, description="手机号")
    email: Optional[str] = Field(None, max_length=100, description="邮箱")
    # 微信相关字段
    openid: Optional[str] = Field(None, max_length=100, description="微信openid")
    wx_avatar: Optional[str] = Field(None, max_length=500, description="微信头像")
    wx_nickname: Optional[str] = Field(None, max_length=50, description="微信昵称")
    wx_id: Optional[str] = Field(None, max_length=50, description="微信号")
    qq_id: Optional[str] = Field(None, max_length=50, description="QQ号")
    # 社交媒体ID
    xhs_id: Optional[str] = Field(None, max_length=50, description="小红书ID")
    dy_id: Optional[str] = Field(None, max_length=50, description="抖音ID")
    wb_id: Optional[str] = Field(None, max_length=50, description="微博ID")
    # 统计数据
    like_num: Optional[int] = Field(0, ge=0, description="点赞数")
    comment_num: Optional[int] = Field(0, ge=0, description="评论数")
    follow_num: Optional[int] = Field(0, ge=0, description="关注数")
    fans_num: Optional[int] = Field(0, ge=0, description="粉丝数")
    video_num: Optional[int] = Field(0, ge=0, description="视频数")
    collect_num: Optional[int] = Field(0, ge=0, description="收藏数")

# 用户更新请求模型
class UserUpdate(BaseModel):
    """用户更新请求模型"""
    username: Optional[str] = Field(None, max_length=50, description="用户名")
    password: Optional[str] = Field(None, max_length=255, description="密码")
    nickname: Optional[str] = Field(None, max_length=50, description="昵称")
    avatar: Optional[str] = Field(None, max_length=500, description="头像")
    background_cover: Optional[str] = Field(None, max_length=500, description="背景封面")
    gender: Optional[int] = Field(None, ge=1, le=3, description="性别 1-男 2-女 3-保密")
    age: Optional[int] = Field(None, ge=0, le=150, description="年龄")
    country: Optional[str] = Field(None, max_length=50, description="国家")
    province: Optional[str] = Field(None, max_length=50, description="省份")
    city: Optional[str] = Field(None, max_length=50, description="城市")
    district: Optional[str] = Field(None, max_length=50, description="区县")
    address: Optional[str] = Field(None, max_length=200, description="地址")
    role: Optional[int] = Field(None, ge=0, le=4, description="角色 0-管理员 1-普通用户 2-评委")
    birthday: Optional[date] = Field(None, description="生日")
    signature: Optional[str] = Field(None, max_length=200, description="签名")
    description: Optional[str] = Field(None, max_length=200, description="描述")
    phone: Optional[str] = Field(None, max_length=11, description="手机号")
    email: Optional[str] = Field(None, max_length=100, description="邮箱")
    # 微信相关字段
    openid: Optional[str] = Field(None, max_length=100, description="微信openid")
    wx_avatar: Optional[str] = Field(None, max_length=500, description="微信头像")
    wx_nickname: Optional[str] = Field(None, max_length=50, description="微信昵称")
    wx_id: Optional[str] = Field(None, max_length=50, description="微信号")
    qq_id: Optional[str] = Field(None, max_length=50, description="QQ号")
    # 社交媒体ID
    xhs_id: Optional[str] = Field(None, max_length=50, description="小红书ID")
    dy_id: Optional[str] = Field(None, max_length=50, description="抖音ID")
    wb_id: Optional[str] = Field(None, max_length=50, description="微博ID")
    # 统计数据
    like_num: Optional[int] = Field(None, ge=0, description="点赞数")
    comment_num: Optional[int] = Field(None, ge=0, description="评论数")
    follow_num: Optional[int] = Field(None, ge=0, description="关注数")
    fans_num: Optional[int] = Field(None, ge=0, description="粉丝数")
    video_num: Optional[int] = Field(None, ge=0, description="视频数")
    collect_num: Optional[int] = Field(None, ge=0, description="收藏数")

# 用户响应模型
class UserResponse(BaseModel):
    """用户响应模型"""
    id: int
    username: Optional[str]
    nickname: Optional[str]
    avatar: Optional[str]
    background_cover: Optional[str]
    gender: Optional[int] = None
    age: Optional[int] = None
    country: Optional[str]
    province: Optional[str]
    city: Optional[str]
    district: Optional[str]
    address: Optional[str]
    role: Optional[int] = None
    birthday: Optional[date] = Field(None, description="生日")
    
    @field_validator('birthday', mode='before')
    @classmethod
    def validate_birthday(cls, v):
        """验证生日字段，处理空字符串"""
        if v == '' or v is None:
            return None
        return v
    signature: Optional[str]
    description: Optional[str]
    phone: Optional[str]
    email: Optional[str]
    wx_id: Optional[str]
    qq_id: Optional[str]
    wx_avatar: Optional[str]
    wx_nickname: Optional[str]
    openid: Optional[str]
    xhs_id: Optional[str]
    dy_id: Optional[str]
    wb_id: Optional[str]
    like_num: Optional[int] = None
    comment_num: Optional[int] = None
    follow_num: Optional[int] = None
    fans_num: Optional[int] = None
    video_num: Optional[int] = None
    collect_num: Optional[int] = None
    login_time: Optional[datetime]
    create_time: Optional[str]
    update_time: Optional[str]
    is_delete: bool

    @classmethod
    def from_orm(cls, user):
        """从ORM对象创建UserResponse实例，处理空字符串等特殊情况"""
        data = {
            'id': user.id,
            'username': user.username,
            'nickname': user.nickname,
            'avatar': user.avatar,
            'gender': user.gender if user.gender is not None else None,
            'age': user.age if user.age is not None else None,
            'country': user.country,
            'province': user.province,
            'city': user.city,
            'district': user.district,
            'address': user.address,
            'role': user.role if user.role is not None else None,
            'birthday': user.birthday if user.birthday and str(user.birthday).strip() else None,
            'signature': user.signature,
            'description': user.description,
            'phone': user.phone,
            'email': user.email,
            'wx_id': user.wx_id,
            'qq_id': user.qq_id,
            'wx_avatar': user.wx_avatar,
            'wx_nickname': user.wx_nickname,
            'openid': user.openid,
            'xhs_id': user.xhs_id,
            'dy_id': user.dy_id,
            'wb_id': user.wb_id,
            'like_num': getattr(user, 'like_num', None),
            'comment_num': getattr(user, 'comment_num', None),
            'follow_num': getattr(user, 'follow_num', None),
            'fans_num': getattr(user, 'fans_num', None),
            'video_num': getattr(user, 'video_num', None),
            'collect_num': getattr(user, 'collect_num', None),
            'login_time': user.login_time,
            'create_time': user.create_time.strftime('%Y-%m-%d %H:%M:%S') if user.create_time else None,
            'update_time': user.update_time.strftime('%Y-%m-%d %H:%M:%S') if user.update_time else None,
            'is_delete': user.is_delete
        }
        return cls(**data)

    model_config = ConfigDict(from_attributes=True)

# 用户列表响应模型
class UserListResponse(BaseModel):
    """用户列表响应模型"""
    code: int
    msg: str
    data: List[UserResponse]
    total: int
    page: int
    page_size: int

# 单个用户响应模型
class SingleUserResponse(BaseModel):
    """单个用户响应模型"""
    code: int
    msg: str
    data: Optional[UserResponse] = None

# 重置管理员密码请求模型
class AdminResetPassword(BaseModel):
    """重置管理员密码请求模型"""
    old_password: str = Field(..., min_length=1, max_length=255, description="当前密码")
    new_password: str = Field(..., min_length=6, max_length=255, description="新密码，至少6位")

# 管理员信息更新请求模型
class AdminInfoUpdate(BaseModel):
    """管理员信息更新请求模型"""
    username: Optional[str] = Field(None, max_length=50, description="用户名")
    nickname: Optional[str] = Field(None, max_length=50, description="昵称")
    avatar: Optional[str] = Field(None, max_length=500, description="头像")
    gender: Optional[int] = Field(None, ge=1, le=3, description="性别 1-男 2-女 3-保密")
    birthday: Optional[date] = Field(None, description="生日")
    description: Optional[str] = Field(None, max_length=200, description="描述")
    phone: Optional[str] = Field(None, max_length=11, description="手机号")
    email: Optional[str] = Field(None, max_length=100, description="邮箱")


# 管理员信息更新响应模型
class AdminInfoUpdateResponse(BaseModel):
    """管理员信息更新响应模型"""
    code: int
    msg: str
    data: Optional[UserResponse] = None
